package com.netjava.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.netjava.dao.IAucRecordDAO;
import com.netjava.pojo.AucRecord;
import com.netjava.pojo.Preauction;
import com.netjava.pojo.Product;
import com.netjava.pojo.User;

/**
 * A data access object (DAO) providing persistence and search support for
 * Aucrecord entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.netjava.pojo.AucRecord
 * @author MyEclipse Persistence Tools
 */

public class AucRecordDAO extends HibernateDaoSupport implements IAucRecordDAO{
	private static final Log log = LogFactory.getLog(AucRecordDAO.class);
	// property constants
	public static final String AUC_PRICE = "aucPrice";
	public static final String AUC_TIME = "aucTime";
	public static final String OTHER1 = "other1";

	protected void initDao() {
		// do nothing
	}

	public void save(AucRecord aucRecord) {
		log.debug("saving Aucrecord instance");
		try {
			getHibernateTemplate().save(aucRecord);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(AucRecord aucRecord) {
		log.debug("deleting Aucrecord instance");
		try {
			getHibernateTemplate().delete(aucRecord);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public AucRecord findById(java.lang.Integer id) {
		log.debug("getting Aucrecord instance with id: " + id);
		try {
			AucRecord instance = (AucRecord) getHibernateTemplate().get(
					"com.netjava.pojo.Aucrecord", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(AucRecord instance) {
		log.debug("finding Aucrecord instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Aucrecord instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Aucrecord as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAucPrice(Object aucPrice) {
		return findByProperty(AUC_PRICE, aucPrice);
	}

	public List findByAucTime(Object aucTime) {
		return findByProperty(AUC_TIME, aucTime);
	}

	public List findByOther1(Object other1) {
		return findByProperty(OTHER1, other1);
	}

	public List findAll() {
		log.debug("finding all Aucrecord instances");
		try {
			String queryString = "from Aucrecord";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public AucRecord merge(AucRecord detachedInstance) {
		log.debug("merging Aucrecord instance");
		try {
			AucRecord result = (AucRecord) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(AucRecord instance) {
		log.debug("attaching dirty Aucrecord instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(AucRecord instance) {
		log.debug("attaching clean Aucrecord instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AucRecordDAO getFromApplicationContext(ApplicationContext ctx) {
		return (AucRecordDAO) ctx.getBean("AucrecordDAO");
	}
	/**
	 * 根据product的id查询所有该商品的拍卖纪录，并将这些记录暗降序排列
	 * @param product
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AucRecord> findByProId(final Product product){
		List<AucRecord> list=new ArrayList<AucRecord>();
		
		try{
			list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					String queryString="FROM AucRecord AS aucRecord WHERE aucRecord.product.productId=? ORDER BY aucRecord.id DESC";
					Query query=session.createQuery(queryString);
					query.setInteger(0,product.getProductId());
					return query.list();
				}
				
			});
		}catch(RuntimeException re){
			throw re;
		}
		return list;
	} 
	public List<AucRecord> findByUserId(final User user){
		List<AucRecord> list=new ArrayList<AucRecord>();
		
		try{
			list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					String queryString="FROM AucRecord AS aucRecord WHERE aucRecord.user.userId=? ORDER BY aucRecord.id DESC";
					Query query=session.createQuery(queryString);
					query.setInteger(0,user.getUserId());
					return query.list();
				}
				
			});
			
		}catch(RuntimeException re){
			throw re;
		}
		return list;
	}
	
	//取指定段
	public List<AucRecord> getNumAcu(final User user,final Integer page,final Integer num){
    	List<AucRecord> list=(List<AucRecord>)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String queryString="FROM com.netjava.pojo.AucRecord AS aucRecord WHERE aucRecord.user.userId=? ORDER BY aucRecord.id DESC";
				Query query=session.createQuery(queryString);
				query.setInteger(0, user.getUserId());
				query.setFirstResult((page-1)*num);
				query.setMaxResults(10);
				return query.list();
			}
    		
    	});
    	return list;
    }

}